package com.wuhao.shootclient.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;


public class RoundBorderView extends View {
    private Paint paint;
    private int radius = 0;
    private String mTipsText = "点击按钮进行人脸检测..."; // 文字内容
    private Paint mTextPaint; // 用于绘制文字的画笔
    private float mTextHeight; // 文字高度
    private Paint mBackgroundPaint; // 文字背景底色画笔

    public RoundBorderView(Context context) {
        this(context, null);
    }

    public RoundBorderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initTextPaint();
        initBackgroundPaint();
    }

    // 初始化文字画笔
    private void initTextPaint() {
        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE); // 文字颜色为白色
        mTextPaint.setTextSize(30); // 设置文字大小
        mTextPaint.setTextAlign(Paint.Align.CENTER); // 文字居中对齐
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        mTextHeight = fontMetrics.descent - fontMetrics.ascent; // 计算文字高度
    }

    // 初始化文字背景底色画笔
    private void initBackgroundPaint() {
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setColor(Color.parseColor("#80000000")); // 设置背景底色为半透明的灰色
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (paint == null) {
            paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setAntiAlias(true);
            SweepGradient sweepGradient = new SweepGradient(((float) getWidth() / 2), ((float) getHeight() / 2),
                    new int[]{Color.GREEN, Color.CYAN, Color.BLUE, Color.CYAN, Color.GREEN}, null);
            paint.setShader(sweepGradient);
        }
        drawBorder(canvas, 6);


    }

    private void drawBorder(Canvas canvas, int rectThickness) {
        if (canvas == null) {
            return;
        }
        //绘制圆
        paint.setStrokeWidth(rectThickness);
        Path drawPath = new Path();
        drawPath.addRoundRect(new RectF(0, 0, getWidth(), getHeight()), radius, radius, Path.Direction.CW);
        canvas.drawPath(drawPath, paint);
        // 绘制文字背景底色
        if (mTipsText != null) {
            float textWidth = mTextPaint.measureText(mTipsText); // 计算文字宽度
            float textX = getWidth() / 2; // 文字横坐标为控件宽度的一半
            float textY = getHeight() - (getHeight()/6) + mTextHeight / 2;; // 文字纵坐标为控件高度减去文字高度的一半
            float backgroundLeft = Math.max(0, textX - textWidth / 2 - 10); // 背景左边界，不能小于0
            float backgroundTop = textY - mTextHeight; // 背景上边界
            float backgroundRight = Math.min(getWidth(), textX + textWidth / 2 + 10); // 背景右边界，不能大于控件宽度
            float backgroundBottom = textY + mTextHeight/2; // 背景下边界
            canvas.drawRect(backgroundLeft, backgroundTop, backgroundRight, backgroundBottom, mBackgroundPaint);
        }

        // 在中心绘制文字
        if (mTipsText != null) {
            float textX = getWidth() / 2; // 文字横坐标为控件宽度的一半
            float textY = getHeight() - (getHeight()/6) + mTextHeight / 2; // 文字纵坐标为控件高度减去文字高度的一半
            canvas.drawText(mTipsText, textX, textY, mTextPaint);
        }
    }

    public void turnRound() {
        invalidate();
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    // 设置文字内容
    public void setTipsText(String text,int Color) {
        mTextPaint.setColor(Color);
        mTipsText = text;
        invalidate(); // 刷新视图
    }
}


